package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Random;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ApiStageMaxMin;
import com.ruoyi.system.mapper.ApiStageMaxMinMapper;
import com.ruoyi.system.mapper.MiniActivationCodeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.MiniActivationCode;
import com.ruoyi.system.service.IMiniActivationCodeService;

import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * 激活码Service业务层处理
 * 
 * @author liding
 * @date 2025-03-23
 */
@Service
public class MiniActivationCodeServiceImpl extends ServiceImpl<MiniActivationCodeMapper, MiniActivationCode> implements IMiniActivationCodeService
{

    /**
     * 查询激活码
     * 
     * @param id 激活码主键
     * @return 激活码
     */
    @Override
    public MiniActivationCode selectMiniActivationCodeById(Long id)
    {
        return this.getBaseMapper().selectMiniActivationCodeById(id);
    }

    /**
     * 查询激活码列表
     * 
     * @param miniActivationCode 激活码
     * @return 激活码
     */
    @Override
    public List<MiniActivationCode> selectMiniActivationCodeList(MiniActivationCode miniActivationCode)
    {
        return this.getBaseMapper().selectMiniActivationCodeList(miniActivationCode);
    }

    /**
     * 新增激活码
     * 
     * @param miniActivationCode 激活码
     * @return 结果
     */
    @Override
    public String insertMiniActivationCode(MiniActivationCode miniActivationCode)
    {
        // 获取当前时间戳
        String timestamp = String.valueOf(System.currentTimeMillis());
        // 去除前面两位数字
        if (timestamp.length() > 2) {
            timestamp = timestamp.substring(2);
        }
        // 生成3位随机数字
        Random random = new Random();
        int randomNumber = 100 + random.nextInt(900); // 生成100到999之间的随机数
        // 组合时间戳和随机数字
        String activationCode = timestamp + String.valueOf(randomNumber);
        miniActivationCode.setCode(activationCode); // 设置激活码

        miniActivationCode.setCreateBy(getUsername());
        miniActivationCode.setUpdateBy(getUsername());
        miniActivationCode.setCreateTime(DateUtils.getNowDate());
        int i = this.getBaseMapper().insertMiniActivationCode(miniActivationCode);
        return activationCode;
    }

    /**
     * 修改激活码
     * 
     * @param miniActivationCode 激活码
     * @return 结果
     */
    @Override
    public int updateMiniActivationCode(MiniActivationCode miniActivationCode)
    {
        miniActivationCode.setUpdateBy(getUsername());
        miniActivationCode.setUpdateTime(DateUtils.getNowDate());
        return this.getBaseMapper().updateMiniActivationCode(miniActivationCode);
    }

    /**
     * 批量删除激活码
     * 
     * @param ids 需要删除的激活码主键
     * @return 结果
     */
    @Override
    public int deleteMiniActivationCodeByIds(Long[] ids)
    {
        return this.getBaseMapper().deleteMiniActivationCodeByIds(ids);
    }

    /**
     * 删除激活码信息
     * 
     * @param id 激活码主键
     * @return 结果
     */
    @Override
    public int deleteMiniActivationCodeById(Long id)
    {
        return this.getBaseMapper().deleteMiniActivationCodeById(id);
    }
}
